/*	Author		: Arga
 *	Created On	: Apr 28, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import team8.paacdroid.PAACdroid;
import team8.paacdroid.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class RootActivity extends Activity {
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.menuTripPlanner:
			this.finish();

			// Create new intent
			Intent intentTripPlanner = new Intent(this, PAACdroid.class);
			
			// Directly call the activity
			startActivity(intentTripPlanner);

			return true;
	    case R.id.menuScheduleFinder:
			this.finish();

			// Create new intent
			Intent intentScheduleFinder = new Intent(this, ScheduleFinderActivity.class);
			
			// Directly call the activity
			startActivity(intentScheduleFinder);

			return true;
	    case R.id.menuPrivacyPolicy:
			this.finish();

			// Create new intent
			Intent intentPrivacyPolicy = new Intent(this, PrivacyPolicyActivity.class);
			
			// Directly call the activity
			startActivity(intentPrivacyPolicy);

	        return true;
	    case R.id.menuShowLocation:
			this.finish();

			// Create new intent
			Intent intentShowLocation = new Intent(this, ViewMapActivity.class);
			
			// Directly call the activity
			startActivity(intentShowLocation);

	        return true;
	    case R.id.menuFavorites:
			this.finish();

			// Create new intent
			Intent intentViewFavorites = new Intent(this, ViewFavoritesActivity.class);
			
			// Directly call the activity
			startActivity(intentViewFavorites);

	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	public String sendHttpGetRequest(String strUrl, HashMap<String, String> queryString) throws MalformedURLException, IOException {
		// Combine all the fields together
		String combinedQueryString = "";
		for (String key : queryString.keySet()) {
			combinedQueryString += key + "=" + queryString.get(key) + "&"; 
		}
		
		// Remove the trailing "&"
		combinedQueryString = combinedQueryString.substring(0, combinedQueryString.length()-2);
		
		// Debug
		// combinedQueryString = "txtDeparture=Baum+and+Negley&radDeparture=I&txtArrival=300+S.+Craig+St&radArrival=I&ddlArrDep=D&ddlHours=10&ddlMinutes=45&ddlAmPm=PM&hdnTripDate=4%2F21%2F2011&ddlNumItins=3&ddlTripPreference=T&ddlWalkDist=.25&btnSubmit=Submit&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTU4NDI0ODgxMDt0PDtsPGk8MT47PjtsPHQ8O2w8aTw1Pjs%2BO2w8dDw7bDxpPDA%2BOz47bDx0PDtsPGk8MD47PjtsPHQ8O2w8aTwxPjtpPDM%2BO2k8MTU%2BOz47bDx0PDtsPGk8Mz47PjtsPHQ8O2w8aTwxPjs%2BO2w8dDw7bDxpPDA%2BO2k8MT47aTwyPjs%2BO2w8dDw7bDxpPDA%2BOz47bDx0PHQ8OztsPGk8MD47Pj47Oz47Pj47dDw7bDxpPDA%2BOz47bDx0PHQ8cDxwPGw8RGF0YVZhbHVlRmllbGQ7RGF0YVRleHRGaWVsZDs%2BO2w8cm91dGVQcmludDtEZXNjcmlwdGlvbjs%2BPjs%2BO3Q8aTwxMDE%2BO0A8MSAtIEZyZWVwb3J0IFJvYWQ7MTEgLSBGaW5ldmlldzsxMiAtIE1jS25pZ2h0OzEzIC0gQmVsbGV2dWU7MTQgLSBPaGlvIFZhbGxleTsxNSAtIENoYXJsZXM7MTYgLSBCcmlnaHRvbj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Oz47dDxAMDxwPHA8bDxQYWdlU2l6ZTs%2BO2w8aTw3Pjs%2BPjs%2BOzs7Ozs7Ozs7Oz47Oz47Pj47Pj47Pj47Pj47Pj47Pj47PhEv3AmjGcWjVDROPLEA4wQLFo0v&lblHiddenSearch=&lblHiddenSearchType=&txtHiddenArrivalIndex=&txtHiddenDepartureIndex=&txtHiddenLocationIndex=";
		
		// Write a log 
		Log.v(getString(R.string.app_name), "HTTP GET: " + strUrl + combinedQueryString);

		// Make the request
		URL url = new URL(strUrl + combinedQueryString);
		StringBuilder httpResponse = new StringBuilder();
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;
			while ((strLine = input.readLine()) != null) {
				httpResponse.append(strLine);
			}
			input.close();
		}
		
		// Return result
		return httpResponse.toString();
	}
	
	public String sendHttpPostRequest(String strUrl, HashMap<String, String> queryString) throws ClientProtocolException, MalformedURLException, IOException {
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(strUrl);
		
		// Combine all fields together
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		for (String key : queryString.keySet()) {
			nameValuePairs.add(new BasicNameValuePair(key, queryString.get(key)));
		}
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		
		// Write a log
		Log.v(getString(R.string.app_name), "HTTP POST: " + strUrl + nameValuePairs.toString());

		// Make the request
		HttpResponse response = httpclient.execute(httppost);
		
		// Read the response
		StringBuilder httpResponse = new StringBuilder();
		BufferedReader input = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8192);
		String strLine = null;
		while ((strLine = input.readLine()) != null) {
			httpResponse.append(strLine);
		}
		input.close();
		
		// Debug
		// tvResult.getSettings().setJavaScriptEnabled(true);
		// tvResult.loadData(httpResponse.toString().replaceAll("\\t", ""), "text/html", "utf-8");

		// Return result
		return httpResponse.toString();
	}
	
	public void displayAlert(Context ctx, String alertBoxTitle, String message, String closeButtonText) {
		new AlertDialog.Builder(this)
		.setTitle(alertBoxTitle)
		.setMessage(message)
		.setNeutralButton("Close", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlg, int sumthin) {
			}
		})
		.show();
	}
}
